<template>
  <div class="main">
    <Page @drop="drop"/>
    <div class="btn-box">
      <div class="btn-app btn-common" @click="go">生成项目</div>
      <div class="btn-page btn-common" @click="go">生成单页</div>
    </div>
    
  </div>
</template>

<script>
import Page from './Page.vue'
const axios = require('axios');
export default {
  name: 'HelloWorld',
  props: {
    msg: String
  },
  components: {
    Page,
  },
  data() {
    return {

    }
  },
  methods: {
    go() {
          const data = this.$ls.get('domTree')
          axios(
            {
              method: 'POST',
              url: '/cook/',
              data: data,
              responseType: 'blob',
            }
          ).then(res => {
            // console.log(res.data)
          const content = res.data;
          const blobContent = new Blob([content], {type: "application/octet-stream"});
          const blobUrl = window.URL.createObjectURL(blobContent);
          const filename = res.headers['content-disposition'].split('=')[1].replace(/["|']/g, '');
          downloadFileByBlob(blobUrl, filename)

          function downloadFileByBlob(blobUrl, filename) {
              const eleLink = document.createElement('a')
              eleLink.download = filename
              eleLink.style.display = 'none'
              eleLink.href = blobUrl
              // 触发点击
              document.body.appendChild(eleLink)
              eleLink.click()
              window.URL.revokeObjectURL(eleLink.href)
              // 然后移除
              document.body.removeChild(eleLink)
          }
        })
      },
    drop(e) {
      
      // let element = JSON.parse(e.dataTransfer.getData('text/plain'))
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.main {
  width: 800px;
  height: 450px;
  margin: 50px auto 0;
}
.btn-box {
  margin-top: 10px;
}
.btn-common {
  float: left;
  width: 100px;
  height: 30px;
  line-height: 30px;
  margin: 10px auto;
  box-sizing: border-box;
}
.btn-app {
  background: #7dca7d;
  color: #fff;
  cursor: pointer;
}
.btn-page {
  margin-left: 20px;
  border: 1px solid #7dca7d;
  color: #7dca7d;
  cursor: pointer;
}
</style>
